package com.shop.admin.api;

import com.shop.admin.aspect.interce.PowerCheckInterce;
import com.shop.admin.model.po.Admins;
import com.shop.admin.model.po.dto.AdminsDto;
import com.shop.admin.service.AdminsService;
import com.shop.po.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author banxia
 * @since 2023-02-26
 */
@RestController
@RequestMapping("/admins")
public class AdminsController {


    @Autowired
    private AdminsService adminsService;

    //登录
    @PostMapping("/loginAdmins")
    public R loginAdmins(String account,String password){
        String token = adminsService.loginAdmins(account, password);
        return R.ok("成功登录").data("token",token);
    }


    //添加某个管理员
    @PostMapping("/addAdmins")
    @PowerCheckInterce(needPower = "crud")
    public R addAdmins(@RequestBody AdminsDto adminsDto){
        adminsService.addAdmins(adminsDto);
        return R.ok("成功添加新管理员");
    }

    //更新某个管理
    @PostMapping("/updateAdmins/{id}")
    @PowerCheckInterce(needPower = "crud")
    public R updateAdmins(@RequestBody AdminsDto adminsDto,
                             @PathVariable Long id){
        adminsService.updateAdmins(adminsDto,id);
        return R.ok("成功更新管理员");
    }

    //删除某个管理员
    @PostMapping("/removeAdmins/{id}")
    @PowerCheckInterce(needPower = "crud")
    public R removeAdmins(@PathVariable("id") Long id){
        adminsService.removeAdmins(id);
        return R.ok("成功删除管理员");
    }

    //查看所有的管理员
    @PostMapping("/selectAdmins")
    @PowerCheckInterce(needPower = "c")
    public R selectAdmins(){
        List<Admins> admins = adminsService.selectAdmins();
        return R.ok().data("admins",admins);
    }



}

